#!/usr/bin/env python
# coding=utf-8

class DoublyLinkedBase:
    """提供管理双向链表的基础类"""

    class _Node:
        """轻量级非公开结点类；存储双端队列的结点"""
        __slots__ = '_element', '_prev', 'next'

        def __init__(self, e, p, n):
            self._element = e
            self._prev = p
            self._next = n

    def __init__(self):
        """初始化空链表"""
        self._header, self._tailer = self._Node(None, None, None), self._Node(None, None, None)
        self._header._next, self._tailer._prev = self._tailer, self._header
        self._size = 0

    def __len__(self):
        return self._size

    def is_empty(self):
        return bool(self._size)

    def _insert_between(self, e, predecessor, successor):
        """在前驱结点pre和后继结点suc中插入结点e"""
        newest = self._Node(e, predecessor, successor)
        predecessor._next = newest
        successor._prev = newest
        self._size += 1
        return newest

    def _delete_node(self, node):
        """在双向链表中删除结点node"""
        predecessor = node._prev
        successor = node._next
        predecessor._next = node._next
        successor._prev = node._prev
        self._size -= 1
        node._prev, node._next = None, None
        return node._element

